package org.thingsboard.server.dao.customer;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.CustomerConfig;

import java.util.List;
import java.util.stream.Collectors;
import org.thingsboard.server.dao.model.sql.CustomerConfigEntity;
import org.thingsboard.server.dao.sql.customer.CustomerConfigRepository;

@Service
@Slf4j
public class CustomerConfigServiceImpl implements CustomerConfigService {

    @Autowired
    private CustomerConfigRepository customerConfigRepository;

    @Override
    public List<CustomerConfig> findCustomerConfigByUsername(String loginUser) {
        List<CustomerConfigEntity> customerConfigByUsername = customerConfigRepository.findCustomerConfigByUsername(loginUser);
        List<CustomerConfig> c = customerConfigByUsername.stream().map(i -> {
            CustomerConfig customerConfig = new CustomerConfig();
            BeanUtils.copyProperties(i, customerConfig);
            return customerConfig;
        }).collect(Collectors.toList());
        return c;
    }

    @Autowired
    ObjectMapper objectMapper;

    @Override
    public CustomerConfig findCustomerConfigByOrgUser(String orguser) {
        CustomerConfigEntity s = customerConfigRepository.findCustomerConfigByOrgUser(orguser).get(0);
        CustomerConfig customerConfig = new CustomerConfig();
        BeanUtils.copyProperties(s, customerConfig);
        return customerConfig;

    }
}
